package com.yuexunxinxi.carefree.service.b_praise;

import com.yuexunxinxi.carefree.dao.BPraiseMapper;
import com.yuexunxinxi.carefree.entity.BPraise;
import com.yuexunxinxi.carefree.utils.SnowFlakeUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 作者: 李咨辰
 * 文件名: BPraiseServiceImpl.java
 * 路径: com.yuexunxinxi.carefree.service.praise
 * 时间: 2024-10-23 12:15
 * 版本: 1.0
 * 描述: TODO 点赞
 **/
@Service
public class BPraiseServiceImpl implements BPraiseService {
    /**
     * 调用dao层
     */
    @Resource
    private BPraiseMapper bPraiseMapper;


    /**
     * 新增点赞
     *
     * @param bPraise 点赞数据
     * @return 成功true, 失败false
     */
    @Override
    public boolean controlsPraise(BPraise bPraise) {
        BPraise isPraise = bPraiseMapper.getPraise(bPraise);
        int i;
        if (isPraise == null) {
            bPraise.setId(SnowFlakeUtil.getFlowIdInstance().nextId());
            bPraise.setCreateTime(new Date());
            i = bPraiseMapper.addPraise(bPraise);
        } else if (isPraise.getIsDelete() == 0) {
            i = bPraiseMapper.cancelPraise(isPraise.getId(), new Date());
        } else {
            i = bPraiseMapper.recoverPraise(isPraise.getId(), new Date());
        }

        return i > 0;
    }
}
